package com.xcfw.dorm.seviceimpl;

import com.xcfw.dorm.entity.*;
import com.xcfw.dorm.mapper.AssistantMapper;
import com.xcfw.dorm.mapper.DormAssistanMapper;
import com.xcfw.dorm.mapper.StudentMapper;
import com.xcfw.dorm.sevice.BaseService;
import com.xcfw.dorm.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: Dorm
 * @description: 登录服务层
 * @author: Bruse Queen
 * @create: 2018-04-09 19:35
 **/
@Service
public class LoginServiceImpl extends BaseService {
    @Autowired
    private DormAssistanMapper dormAssistanMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private AssistantMapper assistantMapper;

    public ReturnJson login(String username, String password) {
        ReturnJson result;
        String jwt;

        List<DormAssistan> dormAssistans = dormAssistanMapper.selectActionSql(null, username, password,null);
        List<Assistant> assistants = assistantMapper.selectActionSql(null, username, password);
        List<Student> students = studentMapper.selectActionSql(null, username, password, null,
                null, null, null, null);

        if (dormAssistans.size() != 0) {
            jwt = getJwt(username);
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "宿管登录成功成功！", dormAssistans.get(0));
        } else if (assistants.size() != 0) {
            jwt = getJwt(username);
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "辅导员登录成功成功！", assistants.get(0));
        } else if (students.size() != 0) {
            jwt = getJwt(username);
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "学生登录成功成功！", students.get(0));
        } else {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "登录失败！");
        }
        return result;
    }

    public static String getJwt(String username) {
        return JwtUtil.generateToken(username);
    }
}
